package org.crown.project.api.module.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.crown.common.annotation.NotLogin;
import org.crown.common.utils.bean.BeanUtils;
import org.crown.framework.responses.ResponseVo;
import org.crown.project.api.BaseApi;
import org.crown.project.api.module.demo.domain.Demo;
import org.crown.project.api.module.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 功能：票子
 * </p>
 *
 * @author WuFengSheng
 * @since 2020-03-07
 */
@Slf4j
@NotLogin
@Api(tags = "表名：demo 功能：票子")
@RestController
@RequestMapping(BaseApi.API_PATH_PREFIX + DemoApi.API_VERSION + "/demo")
public class DemoApi extends BaseApi {

    /**
     * 接口版本
     */
    public static final String API_VERSION = "v1";

    @Autowired
    private DemoService demoService;

    /**
     * localhost:8088/api/mini/v1/demo/getById?id=1
     *
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("getById")
    public ResponseVo getById(@RequestParam("id") Long id){
        Demo demo2 = new Demo();
        demo2.setNickname("呢称" + System.currentTimeMillis());

        Demo demo1 = demoService.getById(id);
        if(demo1 != null){
            // 将demo2 非null属性复制到demo1中
            BeanUtils.copyNoValueProperties(demo1,demo2);
            boolean flag = demoService.updateById(demo1);
            log.info("demo={}", demo1);
            return ResponseVo.success(demo1);
        }
        return ResponseVo.fail();
    }

    /**
     * localhost:8088/api/mini/v1/demo/list
     *
     * 查询表表
     * @return
     */
    @GetMapping("list")
    public ResponseVo list(){
        Long userId = getUserId();
        log.info("userId={}", userId);
        QueryWrapper queryWrapper = new QueryWrapper();
        List<Demo> list = demoService.list(queryWrapper);
        if(list != null){
            return ResponseVo.success(list);
        }
        return ResponseVo.fail();
    }

    /**
     * localhost:8088/api/mini/v1/demo/page?pageNum=1&pageSize=10&id=2
     *
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param demo
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("page")
    public ResponseVo page(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize, @ApiParam("demo") Demo demo){
        Long userId = getUserId();
        log.info("userId={}, demo={}", userId, demo);
        PageInfo<Demo> page = demoService.page(pageNum, pageSize, demo);
        return ResponseVo.success(page);
    }

    /**
     * 保存或更新信息
     * @param demo
     * @return
     */
    @ApiOperation("保存或更新信息")
    @PostMapping("saveOrUpdate")
    public ResponseVo saveOrUpdate(@ApiParam("demo") Demo demo){
        log.info("demo={}", demo);
        if(demoService.saveOrUpdate(demo)){
            return ResponseVo.success(demo);
        }
        return ResponseVo.fail();
    }

}

